#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"
#include "cloud_ini.h"

//#define    INI_PATH    "/home/wangyuanjie/mqtt_program/cloud_config/cloud.ini"

int cloud_ini_get(const char *ini_file, cloud_ini_t  *cloud_ini)
{
	dictionary     *ini=NULL;
	char           *str=NULL;

	if(!ini_file||!cloud_ini)
	{
		printf("ERROR:ini_file or cloud_ini NULL!\n");
		return 0;
	}

	ini= iniparser_load(ini_file);
	if(ini==NULL)
	{
		printf("ini failure:%s\n",strerror(errno));
		return -1;
	}

	str=iniparser_getstring(ini,"mqtt_config:clientid","NULL");
	strncpy(cloud_ini->clientid,str,strlen(str));
	printf("strlen[24]:%d id:%s \n", strlen(str), cloud_ini->clientid);

	str=iniparser_getstring(ini,"mqtt_config:HOST","NULL");
	printf("strlen[27]:%d \n", strlen(str));
	strncpy(cloud_ini->HOST,str,strlen(str));
	printf("strlen[28]:%d host : %s\n", strlen(str),cloud_ini->HOST);
	
	str=iniparser_getstring(ini,"mqtt_config:PORT","NULL");
	strncpy(cloud_ini->PORT,str,strlen(str));
	printf("strlen[32]:%d\n", strlen(str));

	str=iniparser_getstring(ini,"mqtt_config:topic_push","NULL");
	strncpy(cloud_ini->topic_push,str,strlen(str));
	printf("strlen[36]:%d\n", strlen(str));

	str=iniparser_getstring(ini,"mqtt_config:password","NULL");
	strncpy(cloud_ini->password,str,strlen(str));
	printf("strlen[40]:%d pw:%s\n", strlen(str), cloud_ini->password);

	str=iniparser_getstring(ini,"mqtt_config:username","NULL");
	strncpy(cloud_ini->username,str,strlen(str));
	printf("strlen[44]:%d\n", strlen(str));

	str=iniparser_getstring(ini,"mqtt_config:sqlite_topic","NULL");
	strncpy(cloud_ini->sqlite_topic,str,strlen(str));
	printf("strlen[48]:%d\n", strlen(str));

	str=iniparser_getstring(ini,"mqtt_config:topic_sub","NULL");
	strncpy(cloud_ini->topic_sub,str,strlen(str));
	printf("strlen[52]:%d\n", strlen(str));

	iniparser_freedict(ini);
	return 0;

}
